package com.xingdata.jjxc.activity;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.amap.api.navi.AMapNavi;
import com.amap.api.navi.AMapNaviView;
import com.amap.api.navi.AMapNaviViewOptions;
import com.xingdata.jjxc.App;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.activity.viewdata.NaviEvent;
import com.xingdata.jjxc.ar_navi.ARHudComm;
import com.xingdata.jjxc.ar_navi.ProtoMsg;

import java.util.Timer;
import java.util.TimerTask;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * Created by Administrator on 2016/11/3.
 */

public class NaviStatusViewActivity extends Activity {
    private static final String TAG = "NaviStatusViewActivity";
    AMapNaviView overView;
    AMapNaviView currentView;
    Handler handler =new Handler();
    Timer timer;
    TextView tvPlaceName;
    TextView tvPlaceAddress;
    TextView tvPlaceDistance;

    // 底部导航控制面板
    View naviContorlPanel;

    boolean overviewInTouch = false;
    boolean currentViewInTouch = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.ds3_navi_progress_layout);

        tvPlaceName = (TextView)findViewById(R.id.navi_place_name);
        tvPlaceAddress = (TextView)findViewById(R.id.navi_place_address);
        tvPlaceDistance = (TextView)findViewById(R.id.navi_place_distance);

        naviContorlPanel = findViewById(R.id.navi_startGuide_ll);
        naviContorlPanel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showNaviStopDialog();
            }
        });

        overView = (AMapNaviView) findViewById(R.id.overview_navi);
        overView.onCreate(savedInstanceState);
        AMapNaviViewOptions options = overView.getViewOptions();
        options.setLayoutVisible(false);
        options.setScreenAlwaysBright(true);
        overView.setViewOptions(options);
        overView.setFocusableInTouchMode(false);
        overView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                overviewInTouch = true;
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_MOVE:
                        overviewInTouch = true;
                        break;
                    case MotionEvent.ACTION_UP:
                        overviewInTouch = false;
                        break;
                }
                return true;
            }
        });

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                overView.displayOverview();
            }
        },5*1000);

        currentView = (AMapNaviView) findViewById(R.id.current_navi);
        currentView.onCreate(savedInstanceState);
        currentView.setFocusableInTouchMode(false);
        currentView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_MOVE:
                        currentViewInTouch = true;
                        break;
                    case MotionEvent.ACTION_UP:
                        currentViewInTouch = false;
                        break;

                }
                return true;
            }
        });
        AMapNaviViewOptions options2 = currentView.getViewOptions();
        options2.setLayoutVisible(false);
        options2.setScreenAlwaysBright(true);
        currentView.setViewOptions(options2);

        timer =new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if(!overviewInTouch){
                            overView.displayOverview();
                        }
                        if(!currentViewInTouch){
                            currentView.recoverLockMode();
                        }
                    }
                });
            }
        },1000,5*1000);

        if(getIntent().getBundleExtra("data")!=null){
            Bundle data = getIntent().getBundleExtra("data");
            tvPlaceName.setText(data.getString("dest_place_name"));
            tvPlaceAddress.setText(data.getString("dest_address"));
            tvPlaceDistance.setText(data.getString("dest_distance"));
        }
    }

    // 是否取消当前导航
    private void showNaviStopDialog() {
        // lxs 设备是否连接
        final Dialog exitDialog = new Dialog(this, R.style.my_dialog);
        View v = LayoutInflater.from(this).inflate(R.layout.dialog_stop_current_navigation, null);
        v.findViewById(R.id.dialog_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                exitDialog.dismiss();
            }
        });
        v.findViewById(R.id.dialog_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                exitDialog.dismiss();
                EventBus.getDefault().post(new NaviEvent(NaviEvent.StopNavi));

                if(App.deviceSettings.onBoardNavi){
                    ProtoMsg request_stop_navi = new ProtoMsg("stop_on_board_navi","");
                    ARHudComm.getInstance().sendToHud(request_stop_navi.toBytes());
                }

                finish();
            }
        });
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        exitDialog.addContentView(v, params);
        exitDialog.show();
    }
    @Override
    protected void onDestroy() {
        EventBus.getDefault().post(new NaviEvent(NaviEvent.StopNavi));
        EventBus.getDefault().unregister(this);
        super.onDestroy();
        overView.onDestroy();
        currentView.onDestroy();
        timer.purge();
        timer = null;
    }

    @Override
    protected void onPause() {
        super.onPause();
        overView.onPause();
        currentView.onPause();
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void handleNaviEvent(Object event){

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.e(TAG,"BACK KEY CLICK repeat count="+event.getRepeatCount());
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            showNaviStopDialog();
        }
        return false;
    }
}
